//
//  ViewController.m
//  家校互通
//
//  Created by 姬鑫宇 on 15/4/10.
//  Copyright (c) 2015年 姬鑫宇. All rights reserved.
//
#import "ViewController.h"
#import "DSFScanQRCodeViewController.h"
#import "UserInfoViewController.h"
#import "CoModel.h"
#import "APService.h"
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [DSFUserInfo shareUserInstance].isTeachLogin=YES;
    _loginBtn.layer.masksToBounds=YES;
    _loginBtn.layer.cornerRadius=_loginBtn.frame.size.height/2;


    _userNameTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"用户名" attributes:@{NSForegroundColorAttributeName:[UIColor lightTextColor]}];
    _passWordTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"密码" attributes:@{NSForegroundColorAttributeName: [UIColor lightTextColor]}];
    
    UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureAction:)];
    [self.view addGestureRecognizer:tapGesture];
    // Do any additional setup after loading the view, typically from a nib.
}
-(void)tapGestureAction:(UIGestureRecognizer *)gesture
{
    [_userNameTF resignFirstResponder];
    [_passWordTF resignFirstResponder];
}
- (void)viewWillAppear:(BOOL)animated{
    [self.navigationController setNavigationBarHidden:YES];
    [IQKeyboardManager sharedManager].enable = YES;
    [[IQKeyboardManager sharedManager] setShouldPlayInputClicks:YES];
    [[IQKeyboardManager sharedManager] setShouldResignOnTouchOutside:YES];
    [[IQKeyboardManager sharedManager] setShouldShowTextFieldPlaceholder:YES];
    [[IQKeyboardManager sharedManager] setShouldToolbarUsesTextFieldTintColor:YES];
    //    [[IQKeyboardManager sharedManager] setToolbarManageBehaviour:YES];
    [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[IQKeyboardManager sharedManager] setEnable:NO];
    [[IQKeyboardManager sharedManager] setShouldPlayInputClicks:NO];
    [[IQKeyboardManager sharedManager] setShouldResignOnTouchOutside:NO];
    [[IQKeyboardManager sharedManager] setShouldShowTextFieldPlaceholder:NO];
    [[IQKeyboardManager sharedManager] setShouldToolbarUsesTextFieldTintColor:NO];
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
}
- (IBAction)loginPressed:(id)sender{
//    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    int userType = 0;
    if ([DSFUserInfo shareUserInstance].isTeachLogin) {
        userType = 1;
    }else
    {
        userType = 2;
    }
  
    NSString *registrationId = [APService registrationID];

    if (![NSString isBlankString:_userNameTF.text] && ![NSString isBlankString:_passWordTF.text]) {         [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        NSString *idfv = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
        
        [DSFUserInfo userLogin:^(BOOL result, NSError *error) {
            
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            if (result) {
                NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
                
                [defaults setObject:_userNameTF.text forKey:@"username"];
                [defaults setObject:[_passWordTF.text md5] forKey:@"password"];
                [defaults setObject:@(userType) forKey:@"userType"];
                
                [defaults synchronize];
                //登陆成功后页面跳转
                UserInfoViewController *vc = [self.storyboard  instantiateViewControllerWithIdentifier:@"UserInfoViewController"];
                vc.isFromLoginView=YES;
                
                [self.navigationController pushViewController:vc animated:YES];
            }
        } withUsername:_userNameTF.text andPassword:[_passWordTF.text md5] andUserType:userType andPushUserId:registrationId andImei:idfv];
    }else{
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请添加完整的登录信息" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alertView show];
    }
}
- (IBAction)changeUserType:(UISegmentedControl *)sender {
    NSLog(@"sender%@",@(sender.selectedSegmentIndex));
    
        [DSFUserInfo shareUserInstance].isTeachLogin=1-sender.selectedSegmentIndex ;
  
}


#pragma mark  ---跳转扫一扫页面----
- (IBAction)ScanQRBtnAction:(UIButton *)sender {
    DSFScanQRCodeViewController *viewController=[[DSFScanQRCodeViewController alloc] init];
    [self.navigationController pushViewController:viewController animated:YES];
}


@end
